import type { WorkArticleData, WorkTextData } from '@/types/loginType'
import { defineStore } from 'pinia'
import { defineAsyncComponent, ref } from 'vue'
export const useWorkBenchStore = defineStore('useWorkBenchStore', () => {
  const contentIndex = ref(0)
  const articleData = ref<WorkArticleData[]>([])
  // 修改方法
  function assignTextData(tpyeData: WorkArticleData) {
    contentIndex.value++
    articleData.value.push(tpyeData)
  }
  function insertText(name: string, value: string) {
    if (!!articleData.value.find((item) => item.name === name) && true) {
      ;(articleData.value.find((item) => item.name === name) as WorkTextData).content = value
    }
  }
  // 修改方法
  function assignImgData(tpyeData: WorkArticleData) {
    const data = articleData.value[contentIndex.value - 1]
    if (data.name.startsWith('contentText') && (data as WorkTextData).content === '') {
      // 移除前置为空的文本
      removeType(data.name)
    }
    // 添加图像
    articleData.value.push(tpyeData)
    contentIndex.value++
  }

  // 移除指定名字的数据
  function removeType(name: string) {
    const data = articleData.value.find((item) => item.name === name)
    if (data) {
      const index = articleData.value.indexOf(data)
      if (index !== -1) {
        contentIndex.value--
        articleData.value.splice(index, 1)
      }
    }
  }

  return {
    contentIndex,
    articleData,
    insertText,
    assignTextData,
    assignImgData,
    removeType
  }
})
